package cn.com.gridinfo.sooc.core.expand.score.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;

import com.jeremy.arad.utils.Log;
import com.jeremy.arad.utils.StringUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.expand.score.common.CodeConst;
import cn.com.gridinfo.sooc.core.expand.score.dao.ScoreDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;
import cn.com.gridinfo.sooc.utils.widget.ClearEditText;

/**
 * 绑定支付宝界面
 *
 * @author yingchunli
 */
public class BindZFBActivity extends BaseSoocActivity {

    //判断支付宝账号
    private String userEmail = "^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    private String userPhone = "((13[0-9])|(14[0-9])|(15[^4,\\D])|(17[0-9])|(18[0,0-9]))\\d{8}$";

    @InjectView(R.id.zfb_account)
    ClearEditText zfb;//支付宝账号

    @InjectView(R.id.your_name)
    ClearEditText yourName;//您的姓名

    @InjectView(R.id.sooc_pwd)
    ClearEditText pwd;//SOOC密码

    @InjectView(R.id.confirm_bindzfb)
    Button comfirmBtn;//确定按钮

    ScoreDao dao;
    private String zfbzh, zfbxm;//用来拿到上个界面传来的账户和姓名

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bind_zfb);
        ButterKnife.inject(this);
        dao = new ScoreDao(this);
        if (getIntent().hasExtra("ZFB_ZH")) {
            if (!StringUtils.isNull(getIntent().getStringExtra("ZFB_ZH"))) {
                //拿到传来的支付宝账号，进行赋值
                zfbzh = getIntent().getStringExtra("ZFB_ZH");
                zfb.setText(zfbzh);
            }
            if (!StringUtils.isNull(getIntent().getStringExtra("ZFB_XM"))) {
                //拿到传来的支付宝姓名，进行赋值
                zfbxm = getIntent().getStringExtra("ZFB_XM");
                if (zfbxm.length() == 1) {
                    yourName.setText(zfbxm);
                } else if (zfbxm.length() == 2) {
                    String ming = zfbxm.substring(1, 2);//拿到姓名的第二位,名
                    yourName.setText("*" + ming);
                } else if (zfbxm.length() == 3) {
                    String xing = zfbxm.substring(0, 1);//拿姓
                    Log.d("姓" + xing);
                    String ming = zfbxm.substring(2, 3);//拿名最后一个字
                    Log.d("名最后" + ming);
                    yourName.setText(xing + "*" + ming);
                } else {
                    String xing = zfbxm.substring(0, 1);
                    int length = zfbxm.length();
                    String ming = zfbxm.substring(length - 1, length);
                    yourName.setText(xing + "**" + ming);
                }
            }
        }
        //处理同时出现两个叉号的问题
        yourName.requestFocus();
    }

    @Override
    protected void onResume() {
        super.onResume();
        yourName.clearFocus();
//        yourName.setFocusable(false);
        zfb.requestFocus();
        yourName.setFocusable(true);
    }

    /**
     * 点击确定按钮时
     */
    @OnClick(R.id.confirm_bindzfb)
    void onClick() {
        //判断信息填写是否正确

        //支付宝账号不能为空，名字不能为空，SOOC密码不能为空
        if (zfb.getText().toString().trim().length() < 1) {
            MessageUtils.showImageShortToast(this, "支付宝账号不能为空");
        } else if (yourName.getText().toString().trim().length() < 1) {
            MessageUtils.showImageShortToast(this, "名字不能为空");
        } else if (pwd.getText().toString().trim().length() < 1) {
            MessageUtils.showImageShortToast(this, "SOOC密码不能为空");
        } else if ((!zfb.getText().toString().matches(userEmail)) && (!zfb.getText().toString().matches(userPhone))) {
            // 支付宝格式不正确
            MessageUtils.showImageShortToast(this, "支付宝格式不正确");
        } else {
            //信息全部填写完成
            //调用接口，进行验证
            String zh = zfb.getText().toString().trim();
            String zhmc = yourName.getText().toString().trim();
            Log.i("支付宝账号zh->" + zh);
            Log.i("支付宝账号姓名zhmc->" + zhmc);
            //发送请求 绑定支付宝
            dao.bindCount(zh, zhmc, pwd.getText().toString().trim(), CodeConst.BIND_COUNT);
        }
    }

    /**
     * 判断必填内容是否为空，确定按钮处于什么状态
     * 当必填内容全不为空时，注册按钮才处于可点击状态，否则，不可点击
     */
    @OnTextChanged(value = {R.id.zfb_account, R.id.your_name, R.id.sooc_pwd},
            callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void onAfterTextChanged() {
        if (zfb.getText().toString().trim().equals("") || yourName.getText().toString().trim().equals("")
                || pwd.getText().toString().trim().equals("")) {
            comfirmBtn.setEnabled(false);
        } else {
            comfirmBtn.setEnabled(true);
        }
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            switch (requestCode) {
                case CodeConst.BIND_COUNT:
                    //绑定支付宝成功
                    if (dao.getStatus().equals("0")) {
                        MessageUtils.showRightImageShortToast(this, "绑定成功！");
                        Intent intent = new Intent();
                        intent.putExtra("zfb", zfb.getText().toString().trim());
                        setResult(RESULT_OK, intent);//返回到提现界面
                        finish();
                    } else {
                        MessageUtils.showImageShortToast(this, dao.getStatusCode());
                    }
                    break;
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(this, "请求数据异常");
        }
    }

    @Override
    public String setupToolBarTitle() {
        return "支付宝绑定";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    @OnClick(R.id.toolbar_leftbtn)
    void onToolbaLeftClick() {
        finish();
    }
}
